// pages/ticket_detail/ticket_detail.js
var app = getApp();
//引入这个插件，使html内容自动转换成wxml内容
var WxParse = require('../../wxParse/wxParse.js');
Page({
  data: {
    coupon_id: 0,
    coupon_name: '', //名称
 		coupon_price: '', //价格
    coupon_num: '', //数量
    coupon_total: '', //满多少
    coupon_discount: '', //减多少
    coupon_start: '', //开始时间
    coupon_end: '', //结束时间
    coupon_explain: '', //说明

    ishow:true, // 切换支付方式
  },

  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },

  onLoad: function (option) {
    var that = this;
    that.setData({
      coupon_id: option.coupon_id,
    });
    that.loadDetail();
  },

  loadDetail: function () {
    var that = this;
    wx.request({
      url: app.d.hostUrl + '/common/get_coupondetail',
      method: 'get',
      data: {
        id: that.data.coupon_id,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        // console.log(res);
        // --init data 
        var coupon_name = res.data.coupon_name;
        var coupon_price = res.data.coupon_price;
        var coupon_num = res.data.coupon_num;
        var coupon_total = res.data.coupon_total;
        var coupon_discount = res.data.coupon_discount;
        var coupon_start = res.data.coupon_start;
        var coupon_end = res.data.coupon_end;
        var coupon_explain = res.data.coupon_explain;
        var coupon_detail = res.data.coupon_detail;   
        var coupon_detail = res.data.coupon_detail;   
        WxParse.wxParse('coupon_detail', 'html', coupon_detail, that, 0);
        that.setData({
          coupon_name: coupon_name,
          coupon_price: coupon_price,
          coupon_num: coupon_num,
          coupon_total: coupon_total,
          coupon_discount: coupon_discount,
          coupon_start: coupon_start,
          coupon_end: coupon_end,
          coupon_explain: coupon_explain,
        });
      },
      error: function (e) {
        wx.showToast({
          title: '网络异常！',
          icon:"none",
          duration: 2000,
        });
      },

    });
  },

  // 选择支付方式
  checkPay:function(){
    var that = this;
    that.setData({
      ishow: !that.data.ishow,
    });
  },

  // 余额支付
  paymentYe:function(){
    var that = this;
    wx.request({
      url: app.d.hostUrl + '/user/balancepay',
      method: 'post',
      data: {
        id: that.data.coupon_id,
        sessionkey: wx.getStorageSync('sessionkey'),
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res);
        var code = res.data.errcode;
        var msg = res.data.errmsg;
        if(code==0){
          wx.showModal({
            title: msg,
            success (res) {
              wx.redirectTo({
               url: '/pages/users/users'
             })
            }
          });
        }else{
          wx.showToast({
            title: msg,
            icon:"none",
            duration: 2000,
          });
        }
      },
      error: function (e) {
        wx.showToast({
          title: '网络异常！',
          icon:"none",
          duration: 2000,
        });
      },

    });
   
  },

  //微信支付
  paymentWx:function(){
    var that = this;
    wx.request({
      url: app.d.hostUrl + '/user/orderpay',
      method: 'post',
      data: {
        id: that.data.coupon_id,
        sessionkey: wx.getStorageSync('sessionkey'),
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        var payd = res.data.data;
        var reobj = JSON.parse(payd);
        console.log(payd);
        console.log(reobj);
        var timeStamp1 = reobj.timeStamp;
        var nonceStr1 = reobj.nonceStr;
        var package1 = reobj.package;
        var signType1 = reobj.signType;
        var paySign1 = reobj.paySign;
        wx.requestPayment({
          timeStamp: timeStamp1,
          nonceStr: nonceStr1,
          package: package1,
          signType: signType1,
          paySign: paySign1,
          success (res) {
           console.log(res);
           wx.redirectTo({
            url: '/pages/users/users'
          })
         },
          fail (res) { }
        })
        
      },
      error: function (e) {
        wx.showToast({
          title: '网络异常！',
          icon:"none",
          duration: 2000,
        });
      },

    });
  },

  // 拨打电话
  opentel: function () {
    app.gotel();
  },

  /**
 * 用户点击右上角分享
 */
  onShareAppMessage: function () {
    var openid = wx.getStorageSync('openid');
    from: menu;
    return {
      title: '走吧商城',
      path: '/pages/onepage/onepage?openid=' + openid,
      success: function (res) {
        // console.log(openid);      
      },
      fail: function (res) {
        // console.log(res);
      }
    }
  },


})
